/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassys.action;

import cassys.dataaccess.DAOException;
import cassys.datatransferobjects.AssessmentDTO;
import cassys.datatransferobjects.AssessmentItemsDTO;
import cassys.datatransferobjects.ModuleDTO;
import cassys.delegates.AssessmentDelegate;
import cassys.delegates.ModuleDelegate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Priyanka
 */
public class AssessmentAction extends CassysSupport {

    private AssessmentDTO ass;
    private AssessmentItemsDTO assItems;
    private String idAssessment;
    private String listOfassessmentItems;
    private String selectedModuleCode;
    private AssessmentDelegate delegate;
    Random rand = new Random();
    private ArrayList<ModuleDTO> modules;
    private ModuleDelegate mDelegate;
    private ArrayList<AssessmentDTO> list;

    public ArrayList<AssessmentDTO> getList() throws DAOException {

        list = new ArrayList<AssessmentDTO>();
        list = delegate.getAllAssessmentByModule(selectedModuleCode);
        return list;

    }

    public AssessmentDTO getAssessment() throws DAOException {
        ArrayList<AssessmentDTO> list = delegate.getAllAssessmentByModule(getSelectedModuleCode());

        for (AssessmentDTO item : list) {
            if (item.getAssessmentId().equals(getIdAssessment())) {
                return item;
            }
        }
        throw new DAOException();
    }

    public void setList(ArrayList<AssessmentDTO> list) {
        this.list = list;
    }

    public String getSelectedModuleCode() {
        return selectedModuleCode;
    }

    public void setSelectedModuleCode(String selectedModuleCode) {
        this.selectedModuleCode = selectedModuleCode;
    }

    @Override
    public String execute() throws Exception {

        return "addassessment";
    }

    public String add() throws Exception {

        return "addassessment";
    }

    public AssessmentAction() {
        mDelegate = new ModuleDelegate();
        delegate = new AssessmentDelegate();
        ass = new AssessmentDTO();

    }

    public ArrayList<ModuleDTO> getModules() {
        return modules;
    }

    public void setModules(ArrayList<ModuleDTO> modules) {
        this.modules = modules;
    }

    public ArrayList<String> getModulecodelist() throws Exception {
        modules = new ArrayList<ModuleDTO>();
        modules = mDelegate.findAllModules();
        System.out.println("lsit of modiel" + modules);
        ArrayList<String> codes = new ArrayList<String>();
        Iterator<ModuleDTO> iter = modules.iterator();
        while (iter.hasNext()) {
            codes.add(iter.next().getModuleCode());
        }
        return codes;
    }

    public void setListOfassessmentItems(String listOfassessmentItems) {
        this.listOfassessmentItems = listOfassessmentItems;
    }

    public String getListOfassessmentItems() {
        return listOfassessmentItems;
    }

    public String list() throws Exception {

        return "listAssessment";
    }

    public String create() throws Exception {

        //creating the assessment dto objects
        try {
            ass.setAssessmentId("A-" + rand.nextInt());
            ass.setModuleCode(selectedModuleCode);


            /*
             * // creating the assessmentitems DTO objects StringTokenizer
             * token = new StringTokenizer(listOfassessmentItems, ":");
             *
             * while (token.hasMoreTokens()) { String tempToken =
             * token.nextToken(); if (tempToken.length() > 2) { StringTokenizer
             * token1 = new StringTokenizer(tempToken, " "); while
             * (token1.hasMoreTokens()) { AssessmentItemsDTO dto = new
             * AssessmentItemsDTO();
             *
             *
             * dto.setAssessmentItemId("AI-" + rand.nextInt());
             * dto.setAssessmentId(ass.getAssessmentId());
             * dto.setDescription(token1.nextToken());
             * dto.setWeight(Integer.parseInt(token1.nextToken().trim()));
             * dto.setToRange(Integer.parseInt(token1.nextToken().trim()));
             * dto.setFromRange(Integer.parseInt(token1.nextToken().trim())); if
             * (ass != null) { ass.setAssessmentItem(dto); }
             *
             * }
             * }
             */
            delegate.createAssessment(ass);



        } catch (Exception e) {
            System.out.println("" + e.getMessage());
            return "failure";
        }

        return "success";

    }

    public void delete() {

        try {
            ArrayList<AssessmentDTO> list = delegate.getAllAssessmentByModule(getSelectedModuleCode());

            for (AssessmentDTO item : list) {
                if (item.getAssessmentId().equals(getIdAssessment())) {
                    delegate.deleteAssessment(item);
                }
            }

        } catch (DAOException ex) {
            Logger.getLogger(AssessmentAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String viewItems() {
        return "viewItems";
    }

    public String addItems() {
        return "addItems";
    }
    
    public String addItem() throws DAOException {
        
       AssessmentDTO assessment = getAssessment();
       assItems.setAssessmentItemId("AI-" + rand.nextInt());
       assessment.getAssessmentItems().add(assItems);
       delegate.updateAssessment(assessment);
       return "success";
    }    

    public AssessmentDTO getAss() {
        return ass;
    }

    public void setAss(AssessmentDTO ass) {
        this.ass = ass;
    }

    public AssessmentItemsDTO getAssItems() {
        return assItems;
    }

    public void setAssItems(AssessmentItemsDTO assItems) {
        this.assItems = assItems;
    }

    @Override
    public void validate() {
        try {
            if (ass != null) {
                if (ass.getTitle().trim().length() == 0) {
                    LOG.error("Title is mandatory");
                    addFieldError("ass.title", "Title missing");
                }
                //if (ass.getMaximumMarks() == 0) {
                //    LOG.error("setting default value to maximum marks");
                //    ass.setMaximumMarks(100);
                //}
                if (ass.getDescription().trim().length() == 0) {
                    LOG.error("type to short");
                    addFieldError("ass.description", "Description missing");
                }
                if (ass.getToRange() == 0) {
                    LOG.error("setting default value to maximum marks");
                    ass.setToRange(10);
                }
                if (ass.getFromRange() == 0) {
                    LOG.error("setting default value to maximum marks");
                    ass.setFromRange(1);
                }
            }
        } catch (Exception e) {
            System.out.append("" + e);
        }
    }

    public String getIdAssessment() {
        return idAssessment;
    }

    public void setIdAssessment(String idAssessment) {
        this.idAssessment = idAssessment;
    }
}
